package com.example.cpl.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;

public class vp1 extends AppCompatActivity {

    static Button btn_save,btn_clear;
    private int t;
    private TextView tv_out;
    private EditText et_in;
    public static String filename = "MySharedString_vp1";
    private void initWidght() {
//        tv_out = (TextView) findViewById(R.id.tv_out);
//        et_in = (EditText) findViewById(R.id.et_in);
//        btn_save = (Button) findViewById(R.id.btn_save);
//        btn_clear = (Button) findViewById(R.id.btn_clear);
//        tv_data.setMovementMethod(ScrollingMovementMethod.getInstance());
    }
    public String read_SP_Str(String s,String s1) {
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        return  someData.getString(""+s+"",""+s1+"");
    }
    public int read_SP_Int(String s,int s1) {
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        return  someData.getInt(""+s+"",s1);
    }
    public void  write_SP_Str(String s,String s1) {
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putString(""+s+"",""+s1+"");
        editor.commit();// 提交修改
    }
    public void  write_SP_Int(String s,int s1) {
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = someData.edit();      //让文件 可以编辑
        editor.putInt(""+s+"",s1);
        editor.commit();// 提交修改
    }
    public void clear_SP() {
        final SharedPreferences someData = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = someData.edit();
        editor.clear();
        editor.commit();

    }
    public String getTime(){
        long time=System.currentTimeMillis();   //long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format=new SimpleDateFormat("MM-dd HH:mm");
        Date d1=new Date(time);
        String t1=format.format(d1);
        Log.d("TAG",t1);
        return t1;
    }
    private void setSP() {
        int i;
        t= read_SP_Int("参数", 0);
        Log.d("TAG","读取的参数t="+""+t+"");
        tv_out.setText("");
        for (i =1; i <= t ; i++) {
            String sg_data = read_SP_Str("早上" + "" + i + "", "");
            String time_data = read_SP_Str("时间" + "" + i + "", "");
            tv_out.setText(tv_out.getText() + "记录时间 "+""+time_data+""+"\n"+sg_data + "\n\n");
            Log.d("TAG", "早上吃的是 = " + "" + sg_data + "" + "参数=" + "" + i + "");

        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vp1);
//        initWidght();
//        setSP();
//        tv_out.setMovementMethod(ScrollingMovementMethod.getInstance());


//        btn_save.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                t = t + 1;
//                String time=getTime();
//                write_SP_Str("时间" + "" + t + "", time);
//                write_SP_Str("早上" + "" + t + "", "早餐：" + et_in.getText().toString());
//                write_SP_Int("参数", t);
//                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT)
//                        .show();//阴影提示框
////                check_sg = et_sg.getText().toString();
//
//
//                tv_out.setText(tv_out.getText().toString()+ "记录时间 "+""+time+""+"\n早餐：" + "" +  et_in.getText().toString()  + ""+"\n\n");
//
//                int offset=tv_out.getLineCount()*tv_out.getLineHeight();
//                if(offset>tv_out.getHeight()){
//                    tv_out.scrollTo(0,offset-tv_out.getHeight());
//                }
//                Log.d("TAG", "保存的时间= " + "" + time + "" + "  参数= " + "" + t + "");
//                Log.d("TAG", "保存的早上= " + "" + et_in.getText().toString() + "" + "  参数= " + "" + t + "");
//                Log.d("TAG", "SP存储名：时间" + "" + t + "");
//                Log.d("TAG", "SP存储名：早上" + "" + t + "");
//
//            }
//        });
//        btn_clear.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                clear_SP();
//                t=0;
////                check_sg=null;
////                check_tz=null;
//                tv_out.setText("");
//                et_in.setText("");
//            }
//        });




    }
}
